package Server;

import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import entities.EstimationReport;
import entities.login;
import javax.swing.JOptionPane;
import common.MyFile;
import Database.mysqlConnection;
import GUI.serverGUI;
import ocsf.server.*;
import MessagePacks.*;
import Controllers.*;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class EchoServer extends AbstractServer 
{
  //Class variables *************************************************

  	
  	
  /**
   * The default port to listen on.
   */
  final public static int DEFAULT_PORT = 5555;
  private mysqlConnection SqlDB;
  private CProcessRequestController cpReqCon;
  private CProcessController cProCon;
  private ManPowerController mPowerCon;
  private ReportsController reportCon;
  private Scheduler scheduler;  //For Scheduler
  private MessageController msgcon;

  //Constructors ****************************************************
 
  /**
   * Constructs an instance of the echo server.
   *
   * @param port The port number to connect on.
   */
  public EchoServer(int port, String sqlIp, int sqlPort, String sqlUser, String sqlDBName, String sqlPassword) 
  {
    super(port);

    try {
		String hostName = new String("JDBC:mysql://" + sqlIp + ":" + sqlPort + "/" + sqlDBName);
		SqlDB = new mysqlConnection(hostName, sqlUser, sqlPassword);
    } catch (Exception e) {
		JOptionPane.showMessageDialog(serverGUI.mainframe,"ERROR - Could not listen for clients!");
	}
    cpReqCon = new CProcessRequestController();
    cProCon = new CProcessController();
    mPowerCon = new ManPowerController(); 
    reportCon = new ReportsController();
    scheduler = new Scheduler(this,SqlDB);  //For Scheduler
    msgcon = new MessageController(SqlDB);
 }

  
  //Instance methods ************************************************
  
  /**
   * This method handles any messages received from the client.
   *
   * @param msg The message received from the client.
   * @param client The connection from which the message originated.
   */
  public void handleMessageFromClient (Object msg, ConnectionToClient client)
  {
	
	  MessagePack msgpck = (MessagePack) msg;
	  int action= msgpck.getOpCode();

	  switch(action)
	  {
	  	case MessagePack.login:
	  		login(msgpck,client);
	  		break;
	  	case MessagePack.logout:
			logout(msgpck);
	  		break;
	  	case MessagePack.AddNewProcess:
	  		addNewRequest(msgpck, client);	
	  		break;
	  	case MessagePack.process:
	  		process(msgpck,client);
	  		break;
	  	case MessagePack.Detsupervisor:
	  		Detsupervisor(msgpck,client);
	  		break;
	  	case MessagePack.getAllWorkers:
	  		getAllWorkes(msgpck,client);
	  		break;
	  	case MessagePack.freeze:
	  		freezeProcess(msgpck,client);
	  		break;
	  	case MessagePack.unfreeze:
	  		unFreezeProcess(msgpck,client);
	  		break;
	  	case MessagePack.updateStatus:
	  		updateStatus(msgpck);
	  		break;
	  	case MessagePack.updateSupervisor:
	  		updateSupervisor(msgpck);
	  		break;
	  	case MessagePack.updateEstimator:
	  		updateEstimator(msgpck);
	  		break;
	  	case MessagePack.getProcWaitingConf:
	  		processWaitingConf(msgpck,client);
	  		break;
	  	case MessagePack.getProcess:
	  		getOneProcess(msgpck,client);
	  		break;
	  	case MessagePack.getCommittee:
	  		getCommitteeMembers(msgpck,client);
	  		break;
	  	case MessagePack.estimationtime:
	  		EstimatorDays(msgpck,client);
	  		break;
	  	case MessagePack.getDifferentUsers:
	  		getDifferentUsersThenID(msgpck,client);
	  		break;
	  	case MessagePack.updresponsible:
	  		updateResponsible(msgpck);
	  		break;
	  	case MessagePack.changeComMember:
	  		changeComMember(msgpck,client);
	  		break;
	  	case  MessagePack.updatePhase:
	  		updatePhase(msgpck);
	  		break;
	  	case MessagePack.traceProcess:
	  		getProcessForUser(msgpck,client);
	  		break;
	  	case MessagePack.getEsReport:
	  		getEsReport(msgpck,client);
	  		break;
	  	case MessagePack.getEsReportsMem1:
	  		getAllMem1Rep(msgpck,client);
	  		break;
	  	case MessagePack.getEsReportsMem2:
	  		getAllMem2Rep(msgpck,client);
	  		break;
	  	case MessagePack.getTime:
	  		getTimeOfPros(msgpck,client);
	  		break;
	  	case MessagePack.sendreport:
	  		SendEstReport(msgpck,client);
	  		break;
	  	case MessagePack.updateleveltime:
	  		UpdateTimeForLevel(msgpck,client);
	  		break;
	  	case MessagePack.setTester:
	  		setTesterofPros(msgpck);
	  		break;
	  	case MessagePack.testfailreport:
	  		SetTestFailReport(msgpck,client);
	  		break;
	  	case MessagePack.memApproveEstReportRead:
	  		approveEstReadByMem(msgpck,client);
	  		break;
	  	case MessagePack.getAllReports:
	  		getAllRepIds(msgpck,client);
	  		break;
	  	case MessagePack.checkAllReadEstReport:
	  		checkAllReadReport(msgpck,client);
	  		break;
	  	case MessagePack.messageNotification:
	  		msgcon.retrieve((MessageNotificationPack)msgpck,client);
	  		break;
	  	case MessagePack.getworkerslist:
	  		GetAllWorkersList(msgpck,client);
	  		break;
	  	case MessagePack.deleteEsReport:
	  		deleteEsReport(msgpck,client);
	  		break;
	  	case MessagePack.updateUnsusDate:
	  		updateUnsusDate(msgpck,client);
	  		break;
	  	case MessagePack.updateExecLeader:
	  		updateExecLeader(msgpck,client);
	  		break;
	  	case MessagePack.getAllRequests:
	  	   getRequestsFromDB(msgpck,client);
	  		break;
	  	case MessagePack.getAllprocess:
	  		GetAllProcRequests(msgpck,client);
	  		break;
	  	case MessagePack.getAllRoles:
	  		getAllRolesOfProc(msgpck,client);
	  		break;
	  	case MessagePack.updateExecTimeFlag:
	  		updateExecTimeFlag(msgpck);
	  		break;
	  	case MessagePack.getReqFile:
	  		getRequestFile(msgpck,client);
	  		break;
	  	case MessagePack.createNewTimeExec:
	  		createNewTmExecRqst(msgpck);
	  		break;
	  	case MessagePack.getExtensionRequest:
	  		getAndUpdtExtnRqst(msgpck,client);
	  		break;
	  	case MessagePack.addTimeExtension:
	  		break;
	  	case MessagePack.getStatData:
	  		getStatData(msgpck,client);
	  		break;
	  	case MessagePack.getAllProcOfSup:
	  		getAllProcOfSup(msgpck,client);
	  		break;
	  	case MessagePack.getSumOfexte:
	  		GetSumOfExt(msgpck,client);
	  		break;
	  	case MessagePack.getCountOfPref:
	  		GetCountOfPreformance(msgpck,client);
	  		break;
	  	case MessagePack.gettimeleft:
	  		getTimeLeftForProcess(msgpck,client);
	  		break;
	  	case MessagePack.checkTimeExc:
	  		checkUpdate(msgpck,client);
	  		break;
	  }	    
  }
  
  
 private void checkUpdate(MessagePack msgpck, ConnectionToClient client) {
	 checkTimeException temp = (checkTimeException)(msgpck);
	 SqlDB.updateExceptions(temp.getProcID());
 	}


private void getTimeLeftForProcess(MessagePack msgpck, ConnectionToClient client) {
	 GetAllPack gettime = (GetAllPack) msgpck;
		int Num= 0;
			  Num = SqlDB.getTimeLeftForProcess(gettime.getProcID()); 
		  gettime.setNumber(Num);
		  try {
				client.sendToClient(gettime);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
 }
  
  /**
   * function Gets count of performance
   * @param msgpck:MessagePack - contains the suitable pack to handle GetCountOfPreformance
   * @param client:ConnectionToClient
   */
private void GetCountOfPreformance(MessagePack msgpck, ConnectionToClient client) {
		GetAllPack getcount = (GetAllPack) msgpck;
		int Num= 0;
		 try {
			  Num = SqlDB.getcountOfpreformance();
		  	} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		  		}
		  getcount.setNumber(Num);
		  try {
				client.sendToClient(getcount);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
}

/**
 * Function get the sum of extensions
 * @param msgpck:MessagePack - contains the suitable pack to handle GetSumOfExt
 * @param client:ConnectionToClient
 */
private void GetSumOfExt(MessagePack msgpck, ConnectionToClient client) {
	GetAllPack getsum = (GetAllPack) msgpck;
	int Num= 0;
	 try {
		  Num = SqlDB.getSumOfApprovedExtensions();
	  	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	  		}
	  getsum.setNumber(Num);
	  try {
			client.sendToClient(getsum);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}

/**
 * Function get all process for a supervisor
 * @param msgpck:MessagePack - contains the suitable pack to handle getAllProcOfSup
 * @param client:ConnectionToClient
 */
  private void getAllProcOfSup(MessagePack msgpck, ConnectionToClient client) {
	  GetAllPack getAll = (GetAllPack) msgpck;
	  ArrayList<String> NumOfproc=null;
	 try {
		  NumOfproc = SqlDB.getAllsupProcess(getAll.getSupID());
	  	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	  		}
	  getAll.setArray(NumOfproc);
	  try {
			client.sendToClient(getAll);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}

/**
 * Function get status data
 * @param msgpck:MessagePack - contains the suitable pack to handle getStatData
 * @param client:ConnectionToClient
 */
private void getStatData(MessagePack msgpck, ConnectionToClient client) {
	  getStatisticReport temp = (getStatisticReport)msgpck;
	  //Setting NumOfActiveArrayList
	  temp.setNumOfActive(SqlDB.StatNumOfActive(temp.getStartDate(),temp.getEndDate()));
	  temp.setNumOfFreeze(SqlDB.StatNumOfFrozen(temp.getStartDate(),temp.getEndDate()));
	  temp.setDaysOfEnded(SqlDB.StatNumOfEnded(temp.getStartDate(),temp.getEndDate()));
	try {
		client.sendToClient(temp);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
 * Function get an update for extension request
 * @param msgpck:MessagePack - contains the suitable pack to handle getAndUpdtExtnRqst
 * @param client:ConnectionToClient
 */
private void getAndUpdtExtnRqst(MessagePack msgpck, ConnectionToClient client) {
	  GetAllPack getextrqst = (GetAllPack) msgpck;
	  SqlDB.getAndUpdtExtRqst(getextrqst);
	  try {
			client.sendToClient(getextrqst);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	  
	
}

/**
 * Function create new time extension request
 * @param msgpck:MessagePack - contains the suitable pack to handle createNewTmExecRqst
 */
private void createNewTmExecRqst(MessagePack msgpck) {
	  NewTimeRequest newRqst = (NewTimeRequest) msgpck;
	  SqlDB.createNewTmExecRqst(newRqst.getProcID(),newRqst.getPhase(),newRqst.getNumOfDays(),newRqst.getMsg() );
	
}

/**
 * Function update time extension flag
 * @param msgpck:MessagePack - contains the suitable pack to handle updateExecTimeFlag
 */
private void updateExecTimeFlag(MessagePack msgpck) {
	  UpdatePack updPack = (UpdatePack) msgpck;
	  SqlDB.updateExecTimeFlag(updPack.getProcId(),updPack.getResponOk()); 
	
}

/**
 * Function get the requested file
 * @param msgpck:MessagePack - contains the suitable pack to handle getRequestFile
 * @param client:ConnectionToClient
 */
  private void getRequestFile(MessagePack msgpck, ConnectionToClient client) {
	  getReqestFile temp = (getReqestFile)msgpck;
	String fullName = SqlDB.getReqFileName(temp.getReqID());
    if(fullName!=null)
    	{
    	String fileEnd = new String(fullName.substring(fullName.lastIndexOf('.')+1).trim());
    	temp.setFileName(Integer.toString(temp.getReqID()));
    	temp.setFileEnd(fileEnd);
    	temp.setFileFlag(1);
    	}
    temp.setFile(FileGetter(fullName));
    try {
		client.sendToClient(temp);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
 * function get all the role for a process
 * @param msgpck:MessagePack - contains the suitable pack to handle getAllRolesOfProc
 * @param client:ConnectionToClient
 */
private void getAllRolesOfProc(MessagePack msgpck, ConnectionToClient client){

	  ArrayList<String> mMap = new ArrayList<String>();
	  GetAllPack proc = (GetAllPack) msgpck;
	  try {
		mMap = SqlDB.getAllRolesProc(proc.getInitID(),proc.getNumber());
		proc.setArray(mMap);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  try {
			client.sendToClient(proc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
  }
  

  
  /**
   * Function get all of the process requests
   * @param msgpck:MessagePack - contains the suitable pack to handle GetAllProcRequests
   * @param client:ConnectionToClient
   */
private void GetAllProcRequests(MessagePack msgpck, ConnectionToClient client) {
	ArrayList<String> mMap = new ArrayList<String>();
	GetAllPack proc = (GetAllPack) msgpck; 
	try {
		mMap =SqlDB.getAllrequestID();
		proc.setArray(mMap);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		client.sendToClient(proc);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
 /**
  * function get request from DataBase
  * @param msgpck:MessagePack - contains the suitable pack to handle getRequestsFromDB
  * @param client:ConnectionToClient
  */
private void getRequestsFromDB(MessagePack msgpck, ConnectionToClient client) {
	    ArrayList<String> mMap = new ArrayList<String>();
	  	GetAllPack proc = (GetAllPack) msgpck; 
	  	mMap =SqlDB.getRequestsFromDB(proc.getInitID()); 
		proc.setArray(mMap);
		try {
			client.sendToClient(proc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 
}  
  
 /**
  * function update execution leader
  * @param msgpck:MessagePack - contains the suitable pack to handle updateExecLeader
  * @param client:ConnectionToClient
  */
 private void updateExecLeader(MessagePack msgpck, ConnectionToClient client) {
	 UpdatePack updPack = (UpdatePack) msgpck;
	 cProCon.updateExecLdr(updPack.getWorkerId(),updPack.getProcId(), SqlDB);
	
}

/**
 * Function update unSuspend date
 * @param msgpck:MessagePack - contains the suitable pack to handle updateUnsusDate
 * @param client:ConnectionToClient
 */
private void updateUnsusDate(MessagePack msgpck, ConnectionToClient client) {
	  UpdatePack updPack = (UpdatePack) msgpck;
	  SqlDB.updUnsusDate(updPack.getProcId()); 
	 
	
}

/**
 * Function delete estimation report
 * @param msgpck:MessagePack - contains the suitable pack to handle deleteEsReport
 * @param client:ConnectionToClient
 */
private void deleteEsReport(MessagePack msgpck, ConnectionToClient client) {
	 removeEsReport temp = (removeEsReport)(msgpck);
	 int ans=0;
	 ans = reportCon.DelEsReport((temp.getReportID()),SqlDB);
	 temp.setDeleteOk(ans);
	 try {
		client.sendToClient(temp);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
 * Function check if everyone have read the report
 * @param msgpck:MessagePack - contains the suitable pack to handle checkAllReadReport
 * @param client:ConnectionToClient
 */
private void checkAllReadReport(MessagePack msgpck, ConnectionToClient client) {
	 checkAllMemReadEstReport temp = (checkAllMemReadEstReport)(msgpck);
	 int ans=0;
	 ans = reportCon.CheckEstRepRead(temp.getReportId(),SqlDB);
	 temp.setBothRead(ans);
		try {
			System.out.println("Try Send To Client");
			client.sendToClient(temp);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}

/**
 * function get all reports ID
 * @param msgpck:MessagePack - contains the suitable pack to handle getAllRepIds
 * @param client:ConnectionToClient
 */
private void getAllRepIds(MessagePack msgpck, ConnectionToClient client) {
	GetAllPack getIds = (GetAllPack) msgpck;
	getIds.nArray = reportCon.getAllEsReport(SqlDB);
	try {
		client.sendToClient(getIds);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
 * Function approve the estimation report has been read by the committee member
 * @param msgpck:MessagePack - contains the suitable pack to handle approveEstReadByMem
 * @param client:ConnectionToClient
 */
private void approveEstReadByMem(MessagePack msgpck, ConnectionToClient client) {
	 memApproveRepRead tempPck = (memApproveRepRead)(msgpck);
	 reportCon.setRepReadApprove(tempPck.getMemNum(),tempPck.getRepID(),SqlDB);
	
}

/**
 * function sets tester fail report
 * @param msgpck:MessagePack - contains the suitable pack to handle SetTestFailReport
 * @param client:ConnectionToClient
 */
private void SetTestFailReport(MessagePack msgpck , ConnectionToClient client){

	 UpdatePack fail = (UpdatePack) msgpck;
	 SqlDB.updateTestingFailReport(fail.getProcId(), fail.getTestfail());

 }
  
  
/**
 * function sets a tester for a process
 * @param msgpck:MessagePack - contains the suitable pack to handle setTesterofPros
 */
  private void setTesterofPros(MessagePack msgpck) {
	  UpdatePack updPack = (UpdatePack) msgpck;
	  cProCon.setTester(updPack.getProcId(), updPack.getWorkerId(), SqlDB);
	
}

/**
 * Function update time for a level
 * @param msgpck:MessagePack - contains the suitable pack to handle UpdateTimeForLevel
 * @param client:ConnectionToClient
 */
private void UpdateTimeForLevel(MessagePack msgpck, ConnectionToClient client){
	  UpdatePack updPack = (UpdatePack) msgpck;
	  try {
		SqlDB.UpdateTimeForLevel(updPack.getProcId(), updPack.getTime(), updPack.getPhaseNum());
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
  }

/**
 * Function send estimation report
 * @param msgpck:MessagePack - contains the suitable pack to handle SendEstReport
 * @param client:ConnectionToClient
 */
  private void SendEstReport(MessagePack msgpck, ConnectionToClient client){
	  EstReportPack est = (EstReportPack)msgpck;
	  try {
		SqlDB.addEstimationToDB(est.getNewReport().getReportId() ,est.getNewReport().getSystem() ,est.getNewReport().getChangeDesc(),est.getNewReport().getResult() ,est.getNewReport().getLimitataions(),est.getNewReport().getHazards(),est.getNewReport().getEstTime());
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 
  }
  
  /**
   * Function get the time of a process
   * @param msgpck:MessagePack - contains the suitable pack to handle getTimeOfPros
   * @param client:ConnectionToClient
   */
  private void getTimeOfPros(MessagePack msgpck, ConnectionToClient client){
	  GetAllPack CCM = (GetAllPack)msgpck;
	  try {
		CCM.setNumber(SqlDB.getTimeProc(CCM.getInitID()));
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
		
	}
	  try {
		client.sendToClient(CCM);
	} catch (IOException e) {
		e.printStackTrace();
	}
  }
  
  /**
   * function get all the committee member report
   * @param msgpck:MessagePack - contains the suitable pack to handle getAllMem1Rep
   * @param client:ConnectionToClient
   */
  private void getAllMem1Rep(MessagePack msgpck, ConnectionToClient client){
	  GetAllPack CCM = (GetAllPack)msgpck;
	  CCM.nArray=reportCon.getEsReportMem1(SqlDB);
	  try {
		client.sendToClient(CCM);
	} catch (IOException e) {
		e.printStackTrace();
	}
  }
  
  /**
   * function get all the committee member report
   * @param msgpck:MessagePack - contains the suitable pack to handle getAllMem2Rep
   * @param client:ConnectionToClient
   */
  private void getAllMem2Rep(MessagePack msgpck, ConnectionToClient client){
	  GetAllPack CCM = (GetAllPack)msgpck;
	  CCM.nArray=reportCon.getEsReportMem2(SqlDB);
	  try {
		client.sendToClient(CCM);
	} catch (IOException e) {
		e.printStackTrace();
	}
  }
  
  /**
   * Function get estimator report
   * @param msgpck:MessagePack - contains the suitable pack to handle getEsReport
   * @param client:ConnectionToClient
   */
  private void getEsReport(MessagePack msgpck,ConnectionToClient client){
	  getEstimationReport temp = (getEstimationReport)(msgpck);
	  ArrayList<String> tempList;
	  tempList=reportCon.getEsReport((temp.getReport()).getReportId(),SqlDB);
	  temp = new getEstimationReport(new EstimationReport(Integer.parseInt(tempList.get(0)), tempList.get(1), tempList.get(2), tempList.get(3), tempList.get(4),tempList.get(5), Integer.parseInt(tempList.get(6))));
	  try {
		client.sendToClient(temp);
	} catch (IOException e) {
		e.printStackTrace();
	}
  }
 
  /**
   * Function get process for a user
   * @param msgpck:MessagePack - contains the suitable pack to handle getProcessForUser
   * @param client:ConnectionToClient
   */
  private void getProcessForUser(MessagePack msgpck,ConnectionToClient client){
	  	ArrayList<String> mMap = new ArrayList<String>();
	  	GetAllPack proc = (GetAllPack) msgpck; 
		try {
			mMap =SqlDB.getAllProcessForUser(proc.getInitID());
			
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		proc.setArray(mMap);
		try {
			client.sendToClient(proc);
		} catch (IOException e) {
			e.printStackTrace();
		}
  }
  
  /**
   * Function change committee member
   * @param msgpck:MessagePack - contains the suitable pack to handle changeComMember
   * @param client:ConnectionToClient
   */
  private void changeComMember(MessagePack msgpck,ConnectionToClient client){
	  ChangeComMember CCM = (ChangeComMember)msgpck;
	  int ans=mPowerCon.changeComMember(CCM.getNewCmID(),CCM.getNewCmRole(),SqlDB);
	  try {
		client.sendToClient(ans);
	} catch (IOException e) {
		e.printStackTrace();
	}
  }
  
  /**
   * Function update phase
   * @param msgpck:MessagePack - contains the suitable pack to handle updatePhase
   */
  private void updatePhase(MessagePack msgpck) {
		UpdatePack updPack = (UpdatePack) msgpck;
		cProCon.updatePhase(updPack, SqlDB);
		
  }
  
  /**
   * Function update responsible for a phase
   * @param msgpck:MessagePack - contains the suitable pack to handle updateResponsible
   */
	private void updateResponsible(MessagePack msgpck) {
		UpdatePack updPack = (UpdatePack) msgpck;
		cProCon.updateProcessResponsibleOk(updPack.getProcId(),updPack.getResponOk(),SqlDB);
}

/**
 * Function retrieve users that have different ID 
 * @param msgpck:MessagePack - contains the suitable pack to handle getDifferentUsersThenID
 * @param client:ConnectionToClient
 */
	private void getDifferentUsersThenID(MessagePack msgpck,ConnectionToClient client){
		HashMap<String,String> otherWorkers=null;
		DifferentUsersPack gotDifUsers = (DifferentUsersPack)msgpck;
		otherWorkers = mPowerCon.getAllMenDifferentFromID(((DifferentUsersPack)msgpck).getcurID(), ((DifferentUsersPack)msgpck).getpermissionLevel(), SqlDB);
		gotDifUsers.setHashMap(otherWorkers);

		try {
			client.sendToClient(gotDifUsers);
		} catch (IOException e) {
			System.out.println("Failed Sending Back The Different Users");
		}
	}
  
  
  
  
 /**
  * function get the committee member
  * @param msgpck:MessagePack - contains the suitable pack to handle getCommitteeMembers
  * @param client:ConnectionToClient
  */
  private void getCommitteeMembers(MessagePack msgpck,ConnectionToClient client){
	HashMap<String,String> committee=null;
	  committee = mPowerCon.getCommitteeMembers(SqlDB);
	  getCommitteePack haveTheCommittee = new getCommitteePack(committee);
	  try {
		client.sendToClient(haveTheCommittee);
	} catch (IOException e) {
		System.out.println("FailedToSendTheCommitteeBackToClient");
	}
  }
  
/**
 * Function for one process
 * @param msgpck:MessagePack - contains the suitable pack to handle getOneProcess
 * @param client:ConnectionToClient
 */
  private void getOneProcess(MessagePack msgpck, ConnectionToClient client) {
	OneProcessPack pck = (OneProcessPack) msgpck;
	try {
		SqlDB.getAllDataFromProc(pck);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		client.sendToClient(pck);
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}

/**
 * function configured waiting time for a process
 * @param msgpck:MessagePack - contains the suitable pack to handle processWaitingConf
 * @param client:ConnectionToClient
 */
private void processWaitingConf(MessagePack msgpck, ConnectionToClient client) {
		

	HashMap<String, String> mMap = new HashMap<String, String>();
	GetAllPack proc = (GetAllPack) msgpck; 
	try {
		mMap =SqlDB.GetProcessWaitingConf();	
		
	} catch (SQLException e) {
		e.printStackTrace();
	} 
	proc.setnMap(mMap);
	try {
		client.sendToClient(proc);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
 * Function update estimator day estimation
 * @param msgpck:MessagePack - contains the suitable pack to handle EstimatorDays
 * @param client:ConnectionToClient
 */
private void EstimatorDays(MessagePack msgpck, ConnectionToClient client){
	  UpdatePack updPack = (UpdatePack) msgpck;
	  try {
		SqlDB.CreateTimeForLevel(updPack.getProcId(), updPack.getTime());
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
}

/**
 * Function update supervisor
 * @param msgpck:MessagePack - contains the suitable pack to handle updateSupervisor
 */
  private void updateSupervisor(MessagePack msgpck){
	  UpdatePack updPack = (UpdatePack) msgpck;
	  SqlDB.ChangeSupervisor(updPack.getProcId(),updPack.getWorkerId());
  }
  
  /**
   * Function update estimator
   * @param msgpck:MessagePack - contains the suitable pack to handle updateEstimator
   */
  private void updateEstimator(MessagePack msgpck){
	  UpdatePack updPack = (UpdatePack) msgpck;
	  SqlDB.ChangeEstimator(updPack.getProcId(),updPack.getWorkerId());
  } 
  
  /**
   * Function update process status
   * @param msgpck:MessagePack - contains the suitable pack to handle updateStatus
   */
  private void updateStatus(MessagePack msgpck){
	  UpdatePack statusPack = (UpdatePack) msgpck;
	  cProCon.updateStatus(statusPack, SqlDB);
  }
  
  /**
   * Function retrieve all workers
   * @param msgpck:MessagePack - contains the suitable pack to handle getAllWorkes
   * @param client:ConnectionToClient
   */
  private void getAllWorkes(MessagePack msgpck, ConnectionToClient client){
	  	ArrayList<String> mMap = new ArrayList<String>();
	  	GetAllPack proc = (GetAllPack) msgpck; 
		try {
			mMap =SqlDB.GetAllWorkers();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		proc.setArray(mMap);
		try {
			client.sendToClient(proc);
		} catch (IOException e) {
			e.printStackTrace();
		}
}
  
  /**
   * Function retrieve the workers list
   * @param msgpck:MessagePack - contains the suitable pack to handle GetAllWorkersList
   * @param client:ConnectionToClient
   */
  private void GetAllWorkersList(MessagePack msgpck, ConnectionToClient client){
	  ArrayList<String> mMap = new ArrayList<String>();
	  	GetAllPack proc = (GetAllPack) msgpck; 
	  	mMap =SqlDB.GetAllWorkersList(); 
		proc.setArray(mMap);
		try {
			client.sendToClient(proc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

  }
 
  /**
   * function freeze the process
   * @param msgpck:MessagePack - contains the suitable pack to handle FreezeProcess
   * @param client:ConnectionToClient
   */
 private void freezeProcess(MessagePack msgpck, ConnectionToClient client){
	 	ArrayList<String> mMap = new ArrayList<String>();
	  	FreezeUnfreezePack proc = (FreezeUnfreezePack) msgpck; 
		try {
			mMap =SqlDB.GetAllUnFreezeProcess(proc.getSupID());
			
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		proc.setnMap(mMap);
		try {
			client.sendToClient(proc);
		} catch (IOException e) {
			e.printStackTrace();
		}
 } 
 
 /**
  * function unfreeze the process
  * @param msgpck:MessagePack - contains the suitable pack to handle unFreezeProcess
  * @param client:ConnectionToClient
  */
 private void unFreezeProcess(MessagePack msgpck, ConnectionToClient client){
	 	ArrayList<String> mMap = new ArrayList<String>();
	  	FreezeUnfreezePack proc = (FreezeUnfreezePack) msgpck; 
		try {
			mMap =SqlDB.GetAllFreezeProcess();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		proc.setnMap(mMap);
		try {
			client.sendToClient(proc);
		} catch (IOException e) {
			e.printStackTrace();
		}
}
 /**
  * Function Determine supervisor
  * @param msgpck:MessagePack - contains the suitable pack to handle Detsupervisor
  * @param client:ConnectionToClient
  */
  private void Detsupervisor(MessagePack msgpck, ConnectionToClient client){
	  ArrayList<String> mMap = new ArrayList<String>();
	  	GetAllPack proc = (GetAllPack) msgpck; 
		try {
			mMap =SqlDB.GetAllNewProcess();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		proc.setArray(mMap);
		try {
			client.sendToClient(proc);
		} catch (IOException e) {
			e.printStackTrace();
		}
  }
  
  /**
   * 
   * @param msgpck:MessagePack - contains the suitable pack to handle process
   * @param client:ConnectionToClient
   */
  private void process(MessagePack msgpck, ConnectionToClient client) {
	HashMap<String, String> mMap = new HashMap<String, String>();
	ProcessesPack proc = (ProcessesPack) msgpck; 
	try {
		mMap =SqlDB.GetProcess(proc.getUsr().GetUser());	
		
	} catch (SQLException e) {
		e.printStackTrace();
	} 
	proc.setnMap(mMap);
	try {
		client.sendToClient(proc);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
 * Function login
 * @param msgpck:MessagePack  contains the suitable pack to handle login
 * @param client:ConnectionToClient 
 */
private void login(MessagePack msgpck, ConnectionToClient client) {
	
	  LoginPack lgn = (LoginPack) msgpck;
	  int dbAnswer = login.ERROR;
	 
	  try {
		  dbAnswer = SqlDB.getUser(lgn.getUsr().GetUser(),lgn.getUsr().getPassword());
	  } catch (NumberFormatException e) {
			e.printStackTrace();
	  } catch (SQLException e) {
				e.printStackTrace();
	}
	  lgn.getUsr().SetPermissionLvl(dbAnswer);
	  try {
			client.sendToClient(lgn);
		} catch (IOException e) {
			e.printStackTrace();
		}
	
}
  
/**
 * Function logout
 * @param msgpck:MessagePack  contains the suitable pack to handle logout
 */
  private void logout(MessagePack msgpck){
	  LogoutPack lgo = (LogoutPack) msgpck;
	  SqlDB.LogoutUser(lgo.getUsr().GetUser());
  }


 /**
  * function add new request
  * @param msgpck:MessagePack - contains the suitable pack to handle addNewRequest
  * @param client:ConnectionToClient
  */
  private void addNewRequest(MessagePack msgpck,ConnectionToClient client){
		int reqID=0;
	try {
		reqID = cpReqCon.addNewRequest(((NewRequestPack)msgpck).getMyProcess(),SqlDB);

	} catch (SQLException e) {
		System.out.println("AddOfRequestFailure");
	}
	NewRequestPack temp = (NewRequestPack)msgpck;
	//AddFile//////////////////////////////////////////////////////////////////////////////////
	if(((((NewRequestPack)msgpck).getMyProcess()).getFileFlag())==1)
		{
		fileHandler(temp.getMyProcess().getReqFile(),client,reqID,temp.getMyProcess().getFileEnd());
		cpReqCon.addFileToRequest(reqID,Integer.toString(reqID)+"."+temp.getMyProcess().getFileEnd(),SqlDB);
		}
		
	try {
		cProCon.addNewProcess(reqID,((NewRequestPack)msgpck).getMyProcess(),SqlDB);
	} catch (SQLException e1) {
		System.out.println("AddOfProcessFailure");
	}
	
	try {
		client.sendToClient(reqID);
	} catch (IOException e) {
		System.out.println("Failed Send To Client");
	}
  }
  
  
/**
   * This method overrides the one in the superclass.  Called
   * when the server starts listening for connections.
   */
  protected void serverStarted()
  {
	  JOptionPane.showMessageDialog(serverGUI.mainframe,"Server listening for connections on port " + getPort());
  }
  
  /**
   * This method overrides the one in the superclass.  Called
   * when the server stops listening for connections.
   */
  protected void serverStopped()
  {
		JOptionPane.showMessageDialog(serverGUI.mainframe,"Server has stopped listening for connections.");
  }
  
  /**
   * Function handle file
   * @param msg - message
   * @param client
   * @param reqID - request ID
   * @param end
   */
  public void fileHandler(Object msg,ConnectionToClient client,int reqID,String end)
{
	  int fileSize =((MyFile)msg).getSize();	  
	  byte [] picbytearray  =((MyFile)msg).getMybytearray();
	  FileOutputStream out;
	  try
	  	{
		out=new FileOutputStream(Integer.toString(reqID)+"."+end);
		out.write(((MyFile)msg).getMybytearray());
	    out.close();
	  	}
	  catch(Exception e)
	  	{
	      System.out.println("ERROR - FileNotFound!");   
	  	}
	  System.out.println("File received: " + msg + " from " + client);
	  System.out.println("length "+ fileSize);
	  }
  
  /**
   * Function bring the file from the HardDrive
   * @param fileName - File name
   * @return request file
   */
  public MyFile FileGetter(String fileName)
  {  
	  MyFile reqFile= new MyFile(fileName);
	  String LocalfilePath=fileName;
		
	  try{

		      File newFile = new File (LocalfilePath);
		      		      
		      byte [] mybytearray  = new byte [(int)newFile.length()];
		      FileInputStream fis = new FileInputStream(newFile);
		      BufferedInputStream bis = new BufferedInputStream(fis);			  
		    
		      reqFile.initArray(mybytearray.length);
		      reqFile.setSize(mybytearray.length);
		      
		      bis.read(reqFile.getMybytearray(),0,mybytearray.length);
		      //sendToServer(msg);		      
		    }
		catch (Exception e) {
			System.out.println("Error send (Files)msg) to Server");
		}
	  return(reqFile);
  }

}
//End of EchoServer class
